#include <iostream>
#include <queue>
#include <array>

using i64 = long long;

constexpr int N = 1003;

int g[N][N];
bool st[N][N];

int n, m;

int dx[] = {0, -1, 0, 1, 1, -1, 1, -1};
int dy[] = {-1, 0, 1, 0, 1, 1, -1, -1};

void dfs(int x, int y, bool &hasH, bool &hasL) {
    st[x][y] = true;
    for (int i = 0; i < 8; ++i) {
        int tx = x + dx[i], ty = y + dy[i];
        if (tx < 0 || tx >= n || ty < 0 || ty >= m) {
            continue;
        }
        if (g[tx][ty] > g[x][y]) {
            hasH = true;
        } else if (g[tx][ty] < g[x][y]) {
            hasL = true;
        } else {
            dfs(tx, ty, hasH, hasL);
        }
    }
}

void bfs(int x, int y, bool &hasH, bool &hasL) {
    std::queue<std::array<int, 2>> que;
    que.push({x, y});
    st[x][y] = true;
    while (que.size()) {
        auto [x, y] = que.front();
        que.pop();
        for (int i = 0; i < 8; ++i) {
            int tx = x + dx[i], ty = y + dy[i];
            if (tx < 0 || tx >= n || ty < 0 || ty >= m) {
                continue;
            }
            if (g[tx][ty] > g[x][y]) {
                hasH = true;
            } else if (g[tx][ty] < g[x][y]) {
                hasL = true;
            } else if (!st[tx][ty]) {
                st[tx][ty] = true;
                que.push({tx, ty});
            }
        }
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> n >> m;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            std::cin >> g[i][j];
            // std::cout << g[i][j] << " \n"[j == n - 1];
        }
    }

    int H = 0, L = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (!st[i][j]) {
                bool hasH = false, hasL = false;
                bfs(i, j, hasH, hasL);
                // dfs(i, j, hasH, hasL);
                if (!hasL) {
                    ++L;
                    // std::cout << i << " " << j << " L\n";
                }
                if (!hasH) {
                    ++H;
                    // std::cout << i << " " << j << " H\n";
                }
            }
        }
    }
    std::cout << H << " " << L << "\n";

    return 0;
}